package com.siyuanren.gcocp.web;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.siyuanren.gcocp.annotation.ValidCheck;
import com.siyuanren.gcocp.common.util.ValidUtils;
import com.siyuanren.gcocp.common.web.bean.Result;
import com.siyuanren.gcocp.dto.HomeworkQuestionDTO;
import com.siyuanren.gcocp.service.HomeworkService;
import com.siyuanren.gcocp.util.VaildUtil;
import com.siyuanren.gcocp.vo.HomeworkAnswerVO;
import com.siyuanren.gcocp.vo.HomeworkCompleteVo;
import com.siyuanren.gcocp.vo.QueryChapterHomeworkVO;
import com.siyuanren.gcocp.web.wx.BaseWxController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.web
 * Date         2018/4/18
 * Time         14:02
 * Description 课堂作业
 */
@Slf4j
@Controller
@RequestMapping("/homework")
public class HomeworkController extends BaseWxController{
    @Autowired
    HomeworkService service;
    /**
     * 返回章节课堂作业
     * @param queryChapterHomeworkVO
     * @return
     */
    @ResponseBody
    @RequestMapping("getChapterHomework")
    public Result getChapterHomework(QueryChapterHomeworkVO queryChapterHomeworkVO) throws Exception {
        VaildUtil.check(queryChapterHomeworkVO);
        queryChapterHomeworkVO.setUserId(getUserId());
        List<HomeworkQuestionDTO> chapterHomework = service.getChapterHomework(queryChapterHomeworkVO);
        return new Result(chapterHomework);

    }

    /**
     * 完成家庭作业
     */
    @ResponseBody
    @RequestMapping("complete")
    public Result completeHomework(HomeworkCompleteVo vo) throws Exception {
        VaildUtil.check(vo);
        vo.setUserId(getUserId());
        String answer = Base64.decodeStr(vo.getAnswers());
        List<HomeworkAnswerVO> list = JSON.parseArray(answer, HomeworkAnswerVO.class);
        service.saveAnswerHomework(vo, list);
        service.completeHomework(vo);
        return new Result(list);
    }

}
